package com.viettel.core.dao.report;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.viettel.core.entities.cms.GroupCategory;
import com.viettel.core.entities.cms.SmsSendLog;
import com.viettel.core.entities.report.SmsService;
import com.viettel.core.enumtype.GroupCategoryStatus;
import com.viettel.core.exceptions.DataAccessException;
import com.viettel.core.repo.IRepository;

/**
 * Mo ta muc dich cua lop (interface)
 * 
 * @author: HUYNHDC
 * @version: 1.0
 * @since: 16/07/2014 - 10:50:59
 * @description: moi thuc thi phuong thuc getListSmsServiceByGroupId
 */
public class SmsServiceDAOImpl implements SmsServiceDAO {

	private IRepository repo;

	public void setRepo(IRepository repo) {
		this.repo = repo;
	}

	@Override
	public SmsService getSmsServiceById(Integer id) throws DataAccessException {
		return repo.getEntityById(SmsService.class, id);
	}

	@Override
	public SmsService createSmsService(SmsService smsService)
			throws DataAccessException {
		if (smsService == null) {
			throw new IllegalArgumentException("smssendlog");
		}
		repo.create(smsService);
		return repo.getEntityById(SmsService.class, smsService.getId());
	}

	@Override
	public void updateSmsService(SmsService smsService)
			throws DataAccessException {
		if (smsService == null) {
			throw new IllegalArgumentException("smssendlog");
		}
		repo.update(smsService);
	}

	@Override
	public void deleteSmsService(SmsService smsService)
			throws DataAccessException {
		if (smsService == null) {
			throw new IllegalArgumentException("smssendlog");
		}
		repo.delete(smsService);
	}

	@Override
	public List<SmsService> getListSmsServiceByGroupId(Integer id)
			throws DataAccessException {
		List<Object> params = new ArrayList<Object>();
		String sql = "SELECT * FROM SMS_SERVICE  where GROUP_ID = ?  ORDER BY LOWER(name) ";
		params.add(id);

		return repo.getListBySQL(SmsService.class, sql, params);
	}

	@Override
	public List<SmsService> getListSmsServiceByGroupIdAndType(Integer id,
			Integer typeId) throws DataAccessException {
		List<Object> params = new ArrayList<Object>();
		String sql = "";
		if (id > 0 && typeId > 0) {
			sql = "SELECT * FROM SMS_SERVICE  where GROUP_ID = ? And TYPE = ? ORDER BY LOWER(name) ";
			params.add(id);
			params.add(typeId);
		} else if (id > 0) {
			sql = "SELECT * FROM SMS_SERVICE  where GROUP_ID = ? ORDER BY LOWER(name) ";
			params.add(id);
		} else if (typeId > 0) {
			sql = "SELECT * FROM SMS_SERVICE  where TYPE = ? ORDER BY LOWER(name) ";
			params.add(typeId);
		}
		return repo.getListBySQL(SmsService.class, sql, params);
	}
}
